﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Entity;
using MealCardManagement.Data;

namespace MealCardManagement.Client
{
    public partial class MainForm : Form
    {
        private MCMContext Context = new MCMContext();

        public MainForm()
        {        
            InitializeComponent();
            // 狗屎微软
            UserList.SelectUser += UserList_SelectUser;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            UserList.SetUsers(Context.Users);
        }

        private void UserList_NewUser(UserList sender, string name)
        {
            Context.Users.Add(new User(name));
            Context.SaveChanges();
            sender.SetUsers(Context.Users);
        }

        private void UserList_DeleteUser(UserList sender, int id)
        {
            if (MessageBox.Show("该用户持有的饭卡也将一并删除?", "警告", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;

            Context.Users.Remove(Context.Users.Find(id));
            Context.SaveChanges();
            sender.SetUsers(Context.Users);
            UserList_SelectUser(sender, null);
        }

        private void UserList_SelectUser(UserList sender, int? userId)
        {
            if (userId == null)
            {
                CardList.Enabled = false;
                CardList.SetCards(Enumerable.Empty<MealCard>());
            }
            else
            {
                CardList.Enabled = true;
                CardList.SetCards(Context.Users.Include(x => x.MealCards).Single(x => x.Id == userId).MealCards);
            }
        }

        private void CardList_NewCard(CardList sender)
        {
            var user = Context.Users.Find(UserList.SelectedUserId);
            user.CreateNewCard();
            Context.SaveChanges();
            sender.SetCards(user.MealCards);
        }

        private void CardList_DeleteCard(CardList sender, int id)
        {
            Context.MealCards.Remove(Context.MealCards.Find(id));
            Context.SaveChanges();
            CardList.SetCards(Context.Users.Include(x => x.MealCards).Single(x => x.Id == UserList.SelectedUserId).MealCards);
        }

        private void CardList_RechargeCard(CardList sender, int id, decimal money)
        {
            var card = Context.MealCards.Find(id);
            card.Money += money;
            Context.SaveChanges();
            CardList.SetCards(Context.Users.Include(x => x.MealCards).Single(x => x.Id == UserList.SelectedUserId).MealCards);
        }

        private void CardList_ConsumeCard(CardList sender, int id, decimal money)
        {
            var card = Context.MealCards.Find(id);
            card.Money -= money;
            Context.SaveChanges();
            CardList.SetCards(Context.Users.Include(x => x.MealCards).Single(x => x.Id == UserList.SelectedUserId).MealCards);
        }
    }
}
